﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Configuration;
using FlorDeLiz.Utils;
using FlorDeLiz.BusinessEntitites;
using Constants = FlorDeLiz.Utils.Constants;

namespace FlorDeLiz.LogException
{
    internal static class LogExceptionDA
    {      
        public static void GravarLog(Log log)
        {
            MySqlConnection conexao = new MySqlConnection();
            try
            {
                conexao = new MySqlConnection(Properties.Settings.Default.connectionString);

                if (conexao.State != System.Data.ConnectionState.Open)
                {
                    conexao.Open();
                }
                MySqlCommand cmd = new MySqlCommand(Constants.Procedures.SP_INSERT_LOG, conexao);
                MySqlParameter param = new MySqlParameter("P_DT_ERRO", MySqlDbType.DateTime, 0);
                param.Direction = System.Data.ParameterDirection.Input;
                param.Value = log.Data;
                cmd.Parameters.Add(param);
                param = new MySqlParameter("P_MSG_ERRO", MySqlDbType.String, 500);
                param.Direction = System.Data.ParameterDirection.Input;
                param.Value = log.Mensagem;
                cmd.Parameters.Add(param);
                param = new MySqlParameter("P_CLASSE_ERRO", MySqlDbType.String, 100);
                param.Direction = System.Data.ParameterDirection.Input;
                param.Value = log.Classe;
                cmd.Parameters.Add(param);
                param = new MySqlParameter("P_METODO_ERRO", MySqlDbType.String, 100);
                param.Direction = System.Data.ParameterDirection.Input;
                param.Value = log.Metodo;
                cmd.Parameters.Add(param);
                param = new MySqlParameter("P_STACKTRACE_ERRO", MySqlDbType.Text, 0);
                param.Direction = System.Data.ParameterDirection.Input;
                param.Value = log.StackTrace;
                cmd.Parameters.Add(param);

                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.ExecuteNonQuery();
            }
            catch(Exception ex)
            {
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open)
                {
                    conexao.Close();
                }
            }
        }
    }
}
